package sample;

import com.google.common.collect.Lists;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import sample.business.frame.Generator;
import sample.business.frame.SimpleGenerator;
import sample.business.frame.modules.impl.SingleModuleGenerator;
import sample.business.util.TestDriver;
import sample.data.DbInfo;
import sample.data.PathInfo;

import java.net.URL;
import java.util.*;

public class Controller implements Initializable {

    @FXML
    private TextField folderChose;

    @FXML
    private TextField projectName;

    @FXML
    private TextField publishName;

    @FXML
    private ChoiceBox<String> driverBox;

    @FXML
    private TextField url;

    @FXML
    private TextField name;

    @FXML
    private TextField password;

    @FXML
    private TabPane tab;

    @FXML
    private ListView<String> frameSelect;

    @FXML
    private ListView<String> frameConfirm;

    @FXML
    private TextField groupId;

    @FXML
    private TextField artifactId;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        driverBox.setItems(FXCollections.observableList(Arrays.asList("com.mysql.jdbc.Driver"
                ,"oracle.jdbc.driver.OracleDriver")));
        driverBox.getSelectionModel().selectFirst();

        frameSelect.getItems().setAll("JSP","freemarker","struts2",
                "SpringMVC","cxf","spring","JPA","hibernate","mybatis",
                "springJdbc","dbcp","druid");
        frameSelect.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        frameConfirm.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
    }

    public void openFile(){
        FolderApp folderApp = new FolderApp();
        folderChose.setText(folderApp.open());
    }

    public void resetPath(){
        folderChose.clear();
        projectName.clear();
        publishName.clear();
        DataHolder.clearDataMap();
    }

    public void savePath(){
        if(!CheckInfo.check(Lists.newArrayList(folderChose,projectName,publishName,groupId,artifactId))){
            return;
        }
        PathInfo pathInfo = new PathInfo();
        pathInfo.setProjectName(projectName.getText());
        pathInfo.setPublishName(publishName.getText());
        pathInfo.setWorkspace(folderChose.getText());
        pathInfo.setGroupId(groupId.getText());
        pathInfo.setArtifactId(artifactId.getText());
        DataHolder.setDataMap("path",pathInfo);
        AlterApp.info("保存成功");
        tab.getSelectionModel().select(1);
    }

    public void testdb(){
        if(!CheckInfo.check(Lists.newArrayList(driverBox,url,name,password))){
            return;
        }
        Map.Entry<String,String> entry = TestDriver.test(driverBox.getValue(),url.getText(),
                name.getText(),password.getText());
        if("0".equals(entry.getKey())){
            AlterApp.info(entry.getValue());
        }else{
            AlterApp.error(entry.getValue());
        }
    }

    public void confirmdb(){
        if(!CheckInfo.check(Lists.newArrayList(driverBox,url,name,password))){
            return;
        }
        DbInfo dbInfo = new DbInfo();
        dbInfo.setDriver(driverBox.getValue());
        dbInfo.setName(name.getText());
        dbInfo.setPassword(password.getText());
        dbInfo.setUrl(url.getText());
        DataHolder.setDataMap("db",dbInfo);
        AlterApp.info("保存成功");
        Generator generator = new SimpleGenerator();
        try {
            generator.createProject();
            generator.createConfig();
            tab.getSelectionModel().select(2);
        } catch (Exception e) {
            e.printStackTrace();
            AlterApp.error(e.getMessage());
        }

    }

    public void resetdb(){
        driverBox.getSelectionModel().select(0);
        url.clear();
        name.clear();
        password.clear();
        DataHolder.clearData("path");
    }

    public void addFrame(){
        ObservableList<String> items =  frameConfirm.getItems();
        items.add(frameSelect.getSelectionModel().getSelectedItem());
        frameConfirm.setItems(items);
        frameSelect.getItems().remove(frameSelect.getSelectionModel().getSelectedIndex());
        frameSelect.getSelectionModel().select(-1);
    }

    public void removeFrame(){
        String frame = frameConfirm.getSelectionModel().getSelectedItem();
        frameConfirm.getItems().remove(frameConfirm.getSelectionModel().getSelectedIndex());
        frameSelect.getItems().add(frame);
    }

    public void generate(){

    }
}
